package com.jiancong.controller;

import com.github.pagehelper.Page;
import com.jiancong.domain.ResponseResult;
import com.jiancong.domain.dto.RoleQueryCriteria;
import com.jiancong.domain.dto.RoleRequest;
import com.jiancong.domain.dto.RoleStatusRequest;
import com.jiancong.domain.dto.RoleUpdateRequest;
import com.jiancong.domain.entity.Role;
import com.jiancong.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色信息表(Role)控制层
 *
 * @author makejava
 * @since 2024-05-08 19:07:30
 */
@RestController
@RequestMapping("/system")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @GetMapping("/role/list")
    public ResponseResult listAllRoles(@RequestParam(required = false) String roleName,
                                       @RequestParam(required = false) String status,
                                       @RequestParam(defaultValue = "1") Integer pageNum,
                                       @RequestParam(defaultValue = "10") Integer pageSize) {
        RoleQueryCriteria criteria = new RoleQueryCriteria();
        criteria.setPageNum(pageNum);
        criteria.setPageSize(pageSize);
        criteria.setRoleName(roleName);
        criteria.setStatus(status);
        return roleService.getRolePageList(criteria);
    }

    @PutMapping("/role/changeStatus")
    public ResponseResult changeRoleStatus(@RequestBody RoleStatusRequest roleStatusRequest) {
        boolean updated = roleService.changeRoleStatus(roleStatusRequest.getId(), roleStatusRequest.getStatus());
        if (updated) {
            return ResponseResult.okResult();
        } else {
            return ResponseResult.errorResult(500, "操作失败");
        }
    }



    @PostMapping("/role")
    public ResponseResult addRole(@RequestBody RoleRequest roleRequest) {
        boolean added = roleService.addRole(roleRequest);
        if (added) {
            return ResponseResult.okResult();
        } else {
            return ResponseResult.errorResult(500, "操作失败");
        }
    }
    @GetMapping("/role/{id}")
    public ResponseResult getRole(@PathVariable Long id) {
        Role role = roleService.getRoleById(id);
        return ResponseResult.okResult(role);
    }

    @PutMapping("/role")
    public ResponseResult updateRole(@RequestBody RoleUpdateRequest request) {
        boolean updated = roleService.updateRole(request);
        return ResponseResult.okResult(updated);
    }

    @DeleteMapping("/role/{id}")
    public ResponseResult deleteRole(@PathVariable Long id) {
        boolean deleted = roleService.deleteRole(id);
        return ResponseResult.okResult(deleted);
    }

    @GetMapping("/role/listAllRole")
    public ResponseResult listAllRoles() {
        List<Role> roles = roleService.listAllRoles();
        return ResponseResult.okResult(roles);
    }
}





